/*
 * DataSource
 * 
 * Project: Search Engine
 * 
 * Copy right 2012 by Phan Hoàn Vũ
 * Email hoanvuphan@gmail.com
 */

package hcmus.fit.ml.tr.data;

import org.apache.commons.lang3.StringUtils;

import hcmus.fit.ml.tr.data.loader.ArffDataLoader;
import hcmus.fit.ml.tr.data.loader.DataLoader;

/**
 * @author hvp
 * 
 */
public class DataSource {

    private DataLoader loader;

    private String location;

    /**
     * Constructor
     * 
     * @param location
     */
    public DataSource(String location) {
        this.loader = getDataLoader(location);
        this.location = location;
    }

    private DataLoader getDataLoader(String location) {
        if (StringUtils.endsWith(location, ".arff")) {
            this.loader = new ArffDataLoader();
        }
        return loader;
    }

    /**
     * Defined a custom DataLoader
     * 
     * @param loader
     */
    public void setLoader(DataLoader loader) {
        this.loader = loader;
    }

    /**
     * Delivery a DataSet from current DataSource.
     * <p>
     * <b>Note</b> DataSet does not cache in memory
     */
    public Instances getDataSet() {
        return this.loader.load(this.location);
    }

    public static void main(String[] args) {
        DataSource dataSource = new DataSource("D:/temp/letter.arff");
        Instances d = dataSource.getDataSet();
        System.out.println("OK " + d.toString());
    }
}
